package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.impl.GoodsDaoImpl;
import com.qf.pojo.PageBean;
import com.qf.pojo.TbGoods;
import com.qf.pojo.TbGoodsType;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeService;

import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/16 17:41
 */
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao=new GoodsDaoImpl();
    private GoodsTypeService goodsTypeService=new GoodsTypeServiceImpl();
    @Override
    public List<TbGoods> selectByTypeId(int typeId) {
        return goodsDao.selectByTypeId(typeId);
    }

    @Override
    public PageBean<TbGoods> selectByTypeId(long id, int currentPage) {
        PageBean<TbGoods> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        Long totalCount =goodsDao.selectCount(id);
        System.out.println("totalCount = " + totalCount);
        pageBean.setTotalCount(totalCount);
        List<TbGoods> tbGoods =goodsDao.selectList(id,currentPage,pageBean.getPageSize());
        System.out.println("tbGoods = " + tbGoods);
        pageBean.setTotalPage(pageBean.getTotalPage());
        pageBean.setList(tbGoods);
        return pageBean;
    }

    @Override
    public TbGoods findById(long id) {
        return goodsDao.selectById(id);
    }

    /**
     * 查询购物车订单里面的商品
     * @param pid
     * @return
     */
    @Override
    public TbGoods findByGid(long pid) {
        TbGoods tbGoods = goodsDao.findById(pid);
        if (tbGoods != null) {
            GoodsTypeService goodsTypeService=new GoodsTypeServiceImpl();
            long typeid = tbGoods.getTypeid();
            TbGoodsType tbGoodsType =goodsTypeService.selectById(typeid);
            tbGoods.setTbGoodsType(tbGoodsType);
        }
        return tbGoods;
    }

    @Override
    public List<TbGoods> find() {
        List<TbGoods> tbGoods = goodsDao.selectAll();
        for (TbGoods tbGood : tbGoods) {
            long typeid = tbGood.getTypeid();
            TbGoodsType tbGoodsType = goodsTypeService.selectById(typeid);
            tbGood.setTbGoodsType(tbGoodsType);
        }
        return tbGoods;
    }

    @Override
    public void deleteGoods(long id) {
        goodsDao.deleteGoods(id);
    }

    @Override
    public void updateGoods(TbGoods goods) {
        goodsDao.updateGoods(goods);
    }

    @Override
    public void addGoods(TbGoods goods) {
        goodsDao.insertGoods(goods);
    }


}
